/**
 * 
 */
package stc.skymobi.jcr;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import javax.jcr.Item;
import javax.jcr.LoginException;
import javax.jcr.NoSuchWorkspaceException;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;

import org.apache.commons.io.IOUtils;
import org.apache.jackrabbit.rmi.client.ClientRepositoryFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author hp
 *
 */
public class JcrInputStreamBuilder {

    private static final Logger logger 
		= LoggerFactory.getLogger(JcrInputStreamBuilder.class);
    
    private String  uri;
    private String  workspaceName;
    private String  user;
    private String  passwd;
    private String  pathName;
    
    /**
     * @return the path
     */
    public String getPath() {
        return pathName;
    }

    /**
     * @param path the path to set
     */
    public void setPath(String path) {
        this.pathName = path.trim();
    }

    /**
     * @return the uri
     */
    public String getUri() {
        return uri;
    }

    /**
     * @param uri the uri to set
     */
    public void setUri(String uri) {
        this.uri = uri.trim();
    }

    /**
     * @return the workspaceName
     */
    public String getWorkspace() {
        return workspaceName;
    }

    /**
     * @param workspaceName the workspaceName to set
     */
    public void setWorkspace(String workspaceName) {
        this.workspaceName = workspaceName.trim();
    }

    /**
     * @return the user
     */
    public String getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(String user) {
        this.user = user.trim();
    }

    /**
     * @return the passwd
     */
    public String getPasswd() {
        return passwd;
    }

    /**
     * @param passwd the passwd to set
     */
    public void setPasswd(String passwd) {
        this.passwd = passwd.trim();
    }

    InputStream	buildInputStream() {
        Repository 	repository = null;
        ClientRepositoryFactory factory = new ClientRepositoryFactory();
        Session 	session = null;
        InputStream	ret = null;
        try {
            repository = factory.getRepository(uri);
            session = repository.login(
                    new SimpleCredentials(user, passwd.toCharArray()),
                    workspaceName);
            Item item = session.getItem(pathName);
            
            if ( item instanceof Node ) {
                Property  data = ((Node)item).getProperty("jcr:content/jcr:data");
                
                InputStream is = data.getStream();
                byte[] bytes = IOUtils.toByteArray(is);
                IOUtils.closeQuietly(is);
                ret = new ByteArrayInputStream(bytes);
            }
	    } catch (MalformedURLException e) {
	    	logger.error("buildInputStream:", e);
	    } catch (NotBoundException e) {
	    	logger.error("buildInputStream:", e);
	    } catch (RemoteException e) {
	    	logger.error("buildInputStream:", e);
	    } catch (LoginException e) {
	    	logger.error("buildInputStream:", e);
	    } catch (NoSuchWorkspaceException e) {
	    	logger.error("buildInputStream:", e);
	    } catch (RepositoryException e) {
	    	logger.error("buildInputStream:", e);
	    } catch (IOException e) {
	    	logger.error("buildInputStream:", e);
	    }
	    finally {
	        if ( null != session ) {
	            session.logout();
	        }
	    }
	    
	    return	ret;
	}
}
